import { useState, createContext, useContext } from "react";
import { NICKNAME } from "../types";

export type TSocketContext = [
  {
    status: boolean;
    socket: {};
    nickname: string;
  },
  React.Dispatch<
    React.SetStateAction<{
      status: boolean;
      socket: {};
      nickname: string;
    }>
  >
];

const defaultValue: any | TSocketContext = {
  status: false,
  socket: {},
  nickname: localStorage.getItem(NICKNAME),
};

const Context: any = createContext(defaultValue);

const SocketContext = ({ children }: any) => {
  const socketState = useState(defaultValue);

  return <Context.Provider value={socketState}>{children}</Context.Provider>;
};

export default SocketContext;
export const useSocketContext: any | TSocketContext = () => useContext(Context);
